Entity global_followtarget;
float pickupdistance = 10;
float acceleration = 0.2;
float global_followdistance =1;
float total_boxes;
float tut_boxes;
bool start_game;
int total_time;
class Scene:ScriptObject
{
	float vibration_timer;
	float vibration_length;
	float vibration_strength;
	void init()
	{
		global_followtarget = get_entity("rotocopter");
		vibration_timer=0;
		total_boxes = 20;
		tut_boxes = 1;
		start_game = true;
		total_time=0;
	}
	
	void update()
	{
		if (vibration_timer>0)
		{
			vibration_timer-=1.0f/60.0f;
			if (vibration_timer<=0)
			{
				set_bird_vibration(0);
			}
			else
			{
				set_bird_vibration(vibration_timer*pow(vibration_strength/vibration_length,3));
			}
		}
	}
	
	void exit()
	{
		set_bird_vibration(0);
	}
	
	void set_vibration(float strength, float duration)
	{
		vibration_timer=duration;
		vibration_length=duration;
		vibration_strength=strength;
	}
}

class Box:ScriptObject
{
	Entity followtarget;
	Entity launcher;
	Entity launcher2;
	float3 old_position;
	float3 old_position_follow;
	float followdistance;
	float3 launch_speed;
	float3 launch;
	float3 launch2; 
	bool launching;
	int tut_num;
	float base_scale;
	float timer;
	/*int launch_time;
	int timer;*/
	
	
	void init()
	{
		map params = Params;
		followtarget=NULL;
		followdistance = 5;
		old_position = get_world_position(entity);
		launcher = get_entity(params["launcher"].str);
		launch = get_world_position(get_entity(params["launcher"].str));
		set_world_position(entity,launch);
		launch2 = get_world_position(get_entity(params["launcher2"].str));
		launch_speed = (launch-launch2)*5;
		launching = true;
		tut_num = 1;
		start_anim(launcher,2, false);
		Entity particle = get_childentity(launcher, "shooting");
		start_particlesystem(particle);
		/*float4x4 boxmtx=get_world_matrix(entity);
		boxmtx.scale(0.002);
		set_world_matrix(entity,boxmtx);
		timer = 1;
		launch_time = timer;*/
		timer=0;
		base_scale=get_world_matrix(entity).axisX.length();
	}
	
	void update()
	{
		timer+=1/60.0f;
		map params = Params;
		float4x4 boxmtx=get_world_matrix(entity);
		boxmtx.rotate(float3(0,1,0),0.025f);
		boxmtx.normalize();
		boxmtx.scale(base_scale);
		set_world_matrix(entity,boxmtx);
		float3 src=get_world_position(entity);
		if(followtarget==NULL)
		{
			float act_scale=(sin(timer*8))*0.1f;
			boxmtx.normalize();
			boxmtx.scale(act_scale+base_scale);
			set_world_matrix(entity,boxmtx);
			
			if (launching)
			{
			
			/*float scalefact = 1+(1/(52-timer));
			boxmtx.normalize();
			boxmtx.scale(scalefact);
			set_world_matrix(entity,boxmtx);
			timer++;*/
			float3 targetv = ((old_position-src)*0.07f)-launch_speed;
			src+=targetv;
			set_world_position(entity,src);
			launch_speed = launch_speed/2
			;
			
				if (targetv.length() <0.5)
				//if((timer-launch_time)>50)
				{
				/*boxmtx.normalize();
				boxmtx.scale(1);
				set_world_matrix(entity,boxmtx);*/
				launching = false;
				}
			}
			if(!launching)
			{
				int pointer=get_entity("rotocopter");
				Entity particle = get_childentity(launcher, "shooting");
				stop_particlesystem(particle);
				Entity particle_trail = get_childentity(entity, "trail");
				
				
				stop_particlesystem(particle_trail);
				float3 tgt=get_world_position(pointer);
				
			
				if ((tgt - src).length() < pickupdistance )
				{
					followtarget = global_followtarget;
					global_followtarget = entity;
					Tutorial@ tutorial=cast<Tutorial>(Parent);
					tutorial.box_number--;
					total_boxes--;
					tut_num = tut_boxes;
					tut_boxes +=1;
					Scene@ scene=cast<Scene>(get_scriptobject(get_scene_entity()));
					scene.set_vibration(0.3f,0.5f);
					playsfx("box_pickup",false);
					
					
			
					base_scale=0.25;
			
				}
			}
		}
				else
				{
					float slow_down = 0.7f;
					float3 tgt=get_world_position(followtarget);
					if(demo)
					{
					
					base_scale=0.6;
					
					if (followtarget == get_entity("rotocopter"))
						{
						followtarget = get_entity(params["demo_target"].str);
						}			
					}
					
					float3 new_speed = ((tgt-src));
					
					float3 targetmove = tgt-old_position_follow;
					
					src+=(new_speed*((new_speed.length()-followdistance)/new_speed.length()))*slow_down;
					
					set_world_position(entity,src);
					
					old_position_follow = tgt;
					
				}
		
	}
};

class Tutorial:ScriptObject
{
	Entity activating;
	int box_number;
	
	void init()
	{
		box_number = Params["box_number"].i;
		activating = get_entity(Params["activating"].str);
		tut_boxes = 1;
	}
	
	void update()
	{
		if (box_number == 0)
		{
			
			activate(activating);
		}
	}
};
class Clock:ScriptObject
{
	
	int timer;
	int finishing_time;
	int last_seconds;
	bool won;
	float scale_step;
	float scale_target;
	float scale_level;
	
	void init()
	{	
		scale_step = 0.1;
		scale_target = 10;
		scale_level = 1;
		won = false;
		last_seconds = 5;
		demo = false;
		timer = 0;
		finishing_time = 0;
	}
	
	void update()
	{
		set_guitext_text(get_entity("goods_left"),""+total_boxes);
//		print_screen(50,800," Goods left: "+total_boxes);
		int counter = (120-(timer/60));
		if (counter < 0)
		{
		counter = 0;
		}
		int minutes=counter/60;
		if (minutes>0)
			set_guitext_text(get_entity("timeleft_min"),""+minutes+":");
		else
			set_guitext_text(get_entity("timeleft_min"),":");
			
		int secs=counter-60*minutes;
		
		if (secs>9)
			set_guitext_text(get_entity("timeleft_max"),""+secs);
		else
			set_guitext_text(get_entity("timeleft_max"),"0"+secs);
		
		if (!demo)
		{
			if (total_boxes == 0)
			{
			total_time=timer;
		
				
			/*activate(get_entity("youwon"));
			activate(get_entity("alltheboxes"));
			activate(get_entity("amazing"));*/
			activate(get_entity("fadeout"));
			playsfx("win_sound",false);
			demo = true;
			won =true;
			}
			
			timer+=1;
			finishing_time = (timer/60);
			
		
			
			//print_screen(0,350," "+timer/60);
			
			
			
			if (timer/60 >=110)
			{
			float4x4 cmtx=get_world_matrix(entity);
			cmtx.normalize();
			if((timer % 60)==0)
			{
			playsfx("time_up",false);
			}
			if(scale_target == 0)
			{
				if (scale_step == 0.01)
				{
				scale_step = -0.0025;
				scale_target = 40;
				}
				else
				{
				scale_step=0.01;
				scale_target = 10;
				}
			}
			
			scale_target--;
			scale_level+=scale_step;
			cmtx.scale(scale_level*0.3);
			set_world_matrix(entity,cmtx);
			
			
		}
		}
		if (timer/60 >=120)
		{
			if(!demo)
				playsfx("loose_sound",false);
				{	
				demo = true;
				}
			if(!won)
			{
				/*print_screen(500,350," F I N");
				print_screen(150,400," You Collected "+(22-total_boxes)+" boxes out of 22."); 
				print_screen(180,450,"This is Fantastic, well done");*/
			
				activate(get_entity("fadeout"));
				
			}
		}
		if (timer/60 >=130)
		{
			//print_screen(0,380,"MEGVAN "+timer);
//			end_game();
		}
	}
};
class Demo_end:ScriptObject
{
	

	
	void init()
	{	
		
		
	}
	
	void update()
	{
		
		if (demo)
		{
		float4x4 demomtx=get_world_matrix(entity);
		demomtx.rotate(float3(1,1,1),0.01f);
		set_world_matrix(entity,demomtx);
		}
	}
};
class Tutorial_rot:ScriptObject
{
	
	float rot;
	
	void init()
	{	
			map params = Params;
			rot=params["rot"].f;
			tut_boxes = 1;
	}
	
	void update()
	{
		
		if (!demo)
		{
		float4x4 demomtx=get_world_matrix(entity);
		demomtx.rotate(float3(0,1,0),rot);
		set_world_matrix(entity,demomtx);
		
		}
	}
};
class Gadget:ScriptObject
{
	
	float rot;
	int anim_speed;
	bool playing;
	float speed;
	void init()
	{	
			map params = Params;
			anim_speed=params["anim_speed"].f;
			playing = false;
	}
	
	void update()
	{
		
		int pointer=get_entity("rotocopter");
		
		float3 tgt=get_world_position(pointer);
		float3 src=get_world_position(entity);
		
		if ((tgt - src).length() < pickupdistance*3		)
		{

		speed=anim_speed;
		}
		if ((tgt - src).length() >= pickupdistance*3		)
		{
		speed *=0.95;
		
		}
		if(!is_anim_playing(entity)){
		start_anim(entity, speed, true);
		}
		else{
		set_anim_speed(entity, speed);
		}
	}
};
class Finished_time
:ScriptObject
{
	
	float rot;
	int anim_speed;
	bool playing;
	float speed;
	void init()
	{	
			
	}
	
	void update()
	{
		int counter = ((total_time/60));
		if (counter < 0)
		{
		counter = 0;
		}
		int minutes=counter/60;
		if (minutes>0)
			set_guitext_text(get_entity("end_time_min"),minutes+" :");
		else
			set_guitext_text(get_entity("end_time_min"),"0 :");
			
			
		int secs=counter-60*minutes;
		
		if (secs>9)
			set_guitext_text(get_entity("end_time_sec"),""+secs);
		else
			set_guitext_text(get_entity("end_time_sec"),"0"+secs);
		
	}
};
class Gates:ScriptObject
{
	Entity next_gate;
	Entity next_gate_col;
	bool active;
	float timer;
	int delay;
	void init()
	{
		map params = Params;
		delay = params["active_delay"].i;
		//next_gate = get_entity(params["nextgate"].str);
		next_gate_col = get_childentity(entity, "collision");
		active = false; //params["active"].b;
	}
	
	void update()
	{
		timer +=1;
		
		
		
		if(!active)
		{
			/*Entity pointer=get_entity("rotocopter");
			float3 src=get_world_position(entity);	
			float3 tgt=get_world_position(pointer);*/
			if ((timer % (delay+300)) == 0)
			{
			start_anim(entity,-1,false);
			deactivate(next_gate_col);
				/*if((get_scriptobject(next_gate) !is null))
				{
				Gates@ gate=cast<Gates>(get_scriptobject(next_gate));
				
				
				}*/
			active = true;
			}
			
			/*
			if ((tgt - src).length() < pickupdistance )
			{
				
				start_anim(next_gate,-1,false);
				deactivate(next_gate_col);
				if((get_scriptobject(next_gate) !is null))
				{
				Gates@ gate=cast<Gates>(get_scriptobject(next_gate));
				print_screen(0,450,"Megnyitottunk");
				gate.active = true;
				
				}
				active = false;
			}*/
		}
		else
		{
			
			if ((timer % (delay+300)) == 0)
			{
			start_anim(entity,1,false);
			activate(next_gate_col);
				
			active = false;
				
				
			}
		}
	}
	
};



